package com.ruoyi.patrol.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.patrol.domain.PtLineGuard;
import com.ruoyi.patrol.domain.PtLineStaff;
import com.ruoyi.patrol.domain.vo.PtSiteRecordVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patrol.domain.PtLineRecordDetail;
import com.ruoyi.patrol.service.IPtLineRecordDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 巡逻点位记录Controller
 * 
 * @author lkm
 * @date 2022-11-16
 */
@RestController
@RequestMapping("/patrol/detail")
public class PtSiteRecordGuardlController extends BaseController
{
    @Autowired
    private IPtLineRecordDetailService ptLineRecordDetailService;

    /**
     * 查询巡逻点位记录列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_record:list')")
    @GetMapping("/list")
    public AjaxResult list(PtLineRecordDetail ptLineRecordDetail)
    {
        List<PtLineRecordDetail> list = ptLineRecordDetailService.selectPtLineRecordDetailList(ptLineRecordDetail);
        return AjaxResult.success(list);
    }

    /**
     * 查询巡逻点位记录个数,目前PtLineGuard中的参数只支持date，state(0、正常，1、异常，2、关闭)
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_record:list')")
    @GetMapping("/lineFinishDayCount")
    public long getLineFinishDayCount() {
        return ptLineRecordDetailService.selectLineFinishDayCount(SecurityUtils.getSchoolId());
    }

    /**
     * 查询已完成巡查天数
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_record:list')")
    @GetMapping("/count")
    public long count(PtLineRecordDetail ptLineRecordDetail) throws ParseException {
        if (ptLineRecordDetail.getDate() == null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            ptLineRecordDetail.setDate(sdf.parse(sdf.format(new Date())));
        }
        return ptLineRecordDetailService.selectCount(ptLineRecordDetail);
    }

    /**
     * 导出巡逻点位记录列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_record:export')")
    @Log(title = "巡逻点位记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtLineRecordDetail ptLineRecordDetail)
    {
        List<PtLineRecordDetail> list = ptLineRecordDetailService.selectPtLineRecordDetailList(ptLineRecordDetail);
        ExcelUtil<PtLineRecordDetail> util = new ExcelUtil<PtLineRecordDetail>(PtLineRecordDetail.class);
        return util.exportExcel(list, "detail");
    }

    /**
     * 获取巡逻点位记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ptLineRecordDetailService.selectPtLineRecordDetailById(id));
    }

    @PreAuthorize("@ss.hasPermi('patrol:line_record:edit')")
    @Log(title = "巡逻点位记录状态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtLineRecordDetail ptLineRecordDetail)
    {
        PtSiteRecordVo ptLineRecordVo = new PtSiteRecordVo();
        ptLineRecordVo.setRecordId(ptLineRecordDetail.getId());
        ptLineRecordVo.setState(ptLineRecordDetail.getState());
        ptLineRecordVo.setHandleInfo(StringUtils.isEmpty(ptLineRecordDetail.getHandleInfo()) ? null : ptLineRecordDetail.getHandleInfo());

        return toAjax(ptLineRecordDetailService.handleAbnormal(ptLineRecordVo));
    }
}
